﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;

public partial class Genetate : BasePage
{
    TemplateFile tf = new TemplateFile();
    Store st = new Store();
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckLogin();

        if (!IsPostBack)
        {
            ds = tf.GetList("");

            ddltmp.DataSource = ds.Tables[0].DefaultView;
            ddltmp.DataTextField = "tempName";
            ddltmp.DataValueField = "fileName";
            ddltmp.DataBind();
            ddltype.DataSource = ds.Tables[0];
            ddltype.DataTextField = "tempName";
            ddltype.DataValueField = "type";
            ddltype.DataBind();

            ds = st.GetList("");
            ddlStore.DataSource = ds.Tables[0];
            ddlStore.DataTextField = "store";
            ddlStore.DataValueField = "id";
            ddlStore.DataBind();

            ddllogo.DataSource = ds.Tables[0];
            ddllogo.DataTextField = "store";
            ddllogo.DataValueField = "Logo";
            ddllogo.DataBind();

        }
        setContent();

        // Response.Write(tmp);

    }
    private void setContent()
    {

        //ddltmpv.DataSource = ds.Tables[0].DefaultView;
        //ddltmpv.DataTextField = "tempName";
        //ddltmpv.DataValueField = "fileName";
        //ddltmpv.DataBind();


        String tmp = Common.ReadHtml(Server.MapPath("Template//" + ddltmp.SelectedValue));


        ddltype.SelectedIndex = ddltmp.SelectedIndex;
        ddllogo.SelectedIndex = ddlStore.SelectedIndex;

        Category cate = new Category();
        ds = cate.GetList(" storeid=" + ddlStore.SelectedValue);
        StringBuilder catestr = new StringBuilder();
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i]["type"].ToString() == "0")//img
                {
                    catestr.Append(string.Format("<tr><td style='cursor: pointer;display: block; margin: 0;padding: 5px 0;' align='center'><a href='{0}' style='border-top: 1px solid #fff; ' target='_blank'><img src='{1}' border='0'   width='110'></a></td></tr>\n", ds.Tables[0].Rows[i]["catelink"].ToString(), ds.Tables[0].Rows[i]["catevalue"].ToString()));
                }
                else//text
                {
                    catestr.Append(string.Format("<tr><td style='border-top: 1px solid #fff;cursor: pointer;display: block;margin: 0;padding: 5px 0;' align='center'><a href='{0}' style=' display: block; font: 12px/16px Verdana, Geneva, Sans-serif;text-align: left;line-height: 14px;padding: 8px 10px 8px 20px;' target='_blank'>{1}</a></td></tr>\n", ds.Tables[0].Rows[i]["catelink"].ToString(), ds.Tables[0].Rows[i]["catevalue"].ToString()));
                }
            }

        }

        Hot h = new Hot();
        ds = h.GetList(" storeid=" + ddlStore.SelectedValue);
        StringBuilder hotstr = new StringBuilder();
        string hs = " <a href='{0}'><img border='0' style='padding: 2px; border: medium none;' src='{1}'   height='135'    width='135'></a> <span style=' font-size:12px;'><br>{2}</span>\n";

        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                hotstr.Append(string.Format(hs, ds.Tables[0].Rows[i]["url"].ToString(), ds.Tables[0].Rows[i]["img"].ToString(), ds.Tables[0].Rows[i]["text"].ToString()));
            }
        }

        ReleatedItem ri = new ReleatedItem();
        ds = ri.GetList(" storeid=" + ddlStore.SelectedValue);
        StringBuilder ristr = new StringBuilder();
        hs = "<td  style='width:100px;overflow:hidden;' valign='top'><a href='{0}'><img border='0' src='{1}'   height='100'    width='100'></a> <span style='font-size:12px;'><br>{2}</span></td>\n";

        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            ristr.Append("<tr>");
            for (int i = 1; i <= ds.Tables[0].Rows.Count; i++)
            {
                ristr.Append(string.Format(hs, ds.Tables[0].Rows[i - 1]["link"].ToString(), ds.Tables[0].Rows[i - 1]["img"].ToString(), ds.Tables[0].Rows[i - 1]["text"].ToString()));
                if ((i) % 5 == 0)
                {
                    ristr.Append("</tr><tr>");
                }
            }
            ristr.Append("</tr>");
        }

        st.GetModel(int.Parse(ddlStore.SelectedValue));
        tmp = Regex.Replace(tmp, "{%logo%}", "<img src='" + ddllogo.SelectedValue + "' width='935' >");
        tmp = Regex.Replace(tmp, "{%category%}", catestr.ToString());
        tmp = Regex.Replace(tmp, "{%hot%}", hotstr.ToString());
        tmp = Regex.Replace(tmp, "{%releated%}", ristr.ToString());

        if (st.Returns != null && st.Returns.Length > 0)
        {
            tmp = Regex.Replace(tmp, "{%Returns%}", "<img src='" + st.Returns.ToString() + "' width='660' >");
        }
        else
        {
            tmp = Regex.Replace(tmp, "{%Returns%}", "");
        } if (st.Shipping != null && st.Shipping.Length > 0)
        {
            tmp = Regex.Replace(tmp, "{%Shipping%}", "<img src='" + st.Shipping.ToString() + "'  width='660'>");
        }
        else
        {
            tmp = Regex.Replace(tmp, "{%Shipping%}", "");
        } if (st.Payment != null && st.Payment.Length > 0)
        {
            tmp = Regex.Replace(tmp, "{%Payment%}", "<img src='" + st.Payment.ToString() + "'  width='660'>");
        }
        else
        {
            tmp = Regex.Replace(tmp, "{%Payment%}", "");
        } if (st.Replies != null && st.Replies.Length > 0)
        {
            tmp = Regex.Replace(tmp, "{%Replies%}", "<img src='" + st.Replies.ToString() + "'  width='660'>");
        }
        else
        {
            tmp = Regex.Replace(tmp, "{%Replies%}", "");
        } if (st.Feedback != null && st.Feedback.Length > 0)
        {
            tmp = Regex.Replace(tmp, "{%Feedback%}", "<img src='" + st.Feedback.ToString() + "' width='660' >");
        }
        else
        {
            tmp = Regex.Replace(tmp, "{%Feedback%}", "");
        } if (st.FAQ != null && st.FAQ.Length > 0)
        {
            tmp = Regex.Replace(tmp, "{%FAQ%}", "<img src='" + st.FAQ.ToString() + "'  width='660'>");

        }
        else
        {
            tmp = Regex.Replace(tmp, "{%FAQ%}", "");
        }

        hftmp.Value = tmp;


    }

    protected void ddltmp_SelectedIndexChanged(object sender, EventArgs e)
    {
        setContent();
        //String tmp = Common.ReadHtml(Server.MapPath("Template//" + ddltmp.SelectedValue));
    }
    protected void ddlStore_SelectedIndexChanged(object sender, EventArgs e)
    {
        setContent();
        //String tmp = Common.ReadHtml(Server.MapPath("Template//" + ddltmp.SelectedValue));
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".html");
        Response.HeaderEncoding = System.Text.Encoding.GetEncoding(65001);
        Response.Write(txtSource.Text); 
        Response.Flush(); 
        Response.End();
    }
}